package heima3_2.cn.osc.ui.main_fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.oschina.app.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import heima3_2.cn.osc.base.BaseFragment;
import heima3_2.cn.osc.constant.ResultState;
import heima3_2.cn.osc.presenter.SyntheBoggerPresenter;
import heima3_2.cn.osc.presenter.SyntheHotspotPresenter;
import heima3_2.cn.osc.presenter.SynthePresenter;
import heima3_2.cn.osc.presenter.SyntheRecommendPresenter;
import heima3_2.cn.osc.ui.adapter.SyntheAdapter;
import heima3_2.cn.osc.ui.adapter.SyntheBoggerAdapter;
import heima3_2.cn.osc.ui.adapter.SyntheHotspotAdapter;
import heima3_2.cn.osc.ui.adapter.SyntheRecommendAdapter;
import heima3_2.cn.osc.ui.view.AutoLoadListView;

/**
 * Created by ice on 2016/7/19.
 */
public class SyntheFragment extends BaseFragment implements AutoLoadListView.Pagingable {

    public static String TABLAYOUT_FRAGMENT = "tab_fragment";
    @Bind(R.id.lv_synthe)
    public AutoLoadListView mLvSynthe;
    private TextView txt;
    private int type;
    private View mView;
    private SynthePresenter mPresenter;
    public SyntheAdapter mAdapter;
    private View mInflats;
    private SyntheRecommendPresenter mRecommendPresenter;
    private SyntheBoggerPresenter mBoggerPresenter;
    private SyntheHotspotPresenter mHotspotPresenter;
    public SyntheHotspotAdapter mHotspotAdapter;
    public SyntheBoggerAdapter mBoggerAdapter;
    public SyntheRecommendAdapter mRecommendAdapter;

    //TODO:
    int index = 0;  //加载网络的次数，0代表初次加载
    int indexBlog = 0;
    int indexHot = 0;
    int indexRec = 0;


    public static SyntheFragment newInstance(int type) {
        SyntheFragment fragment = new SyntheFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(TABLAYOUT_FRAGMENT, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = (int) getArguments().getSerializable(TABLAYOUT_FRAGMENT);
        }


    }

    @Override
    protected ResultState onLoadSubData() {
        mPresenter = new SynthePresenter(this);
        mPresenter.getData(index);

        getHotspotData();
        getBoggerData();
        getRecommendData();
        return ResultState.LOAD_SUCCESS;
    }

    @Override
    protected View onCreateSubSuccessView(LayoutInflater inflater,
                                          ViewGroup container) {
//        View mInflats = Global.inflats(R.layout.fragment_synthe);
        mInflats = inflater.inflate(R.layout.fragment_synthe,container,false);
        ButterKnife.bind(this, mInflats);

        mAdapter = new SyntheAdapter(getContext(), null);
        mHotspotAdapter = new SyntheHotspotAdapter(getContext(),null);
        mBoggerAdapter = new SyntheBoggerAdapter(getContext(),null);
        mRecommendAdapter = new SyntheRecommendAdapter(getContext(),null);
        initView(mInflats);

        //TODO
        mLvSynthe.setPagingableListener(this);
        mLvSynthe.setHasMoreItems(true);
        return mInflats;
    }


    private void getHotspotData() {
        mHotspotPresenter = new SyntheHotspotPresenter(this);
        mHotspotPresenter.getData(index);
        mLvSynthe.setPagingableListener(new AutoLoadListView.Pagingable() {
            @Override
            public void onLoadMoreItems() {
                mHotspotPresenter.getData(++index);
            }
        });
    }


    private void getBoggerData() {
        mBoggerPresenter = new SyntheBoggerPresenter(this);
        mBoggerPresenter.getData(0);
    }

    private void getRecommendData() {
        mRecommendPresenter = new SyntheRecommendPresenter(this);
        mRecommendPresenter.getData(0);
    }

    private void iniView() {
        mView.findViewById(R.id.lv_synthe);
    }


    protected void initView(View view) {
        //  txt = (TextView) view.findViewById(R.id.tab_txt);


        switch (type) {
            case 1:
                mLvSynthe.setAdapter(mAdapter);
                break;
            case 2:
                mLvSynthe.setAdapter(mHotspotAdapter);
               // txt.setText("这是体育Fragment");
                break;
            case 3:
                mLvSynthe.setAdapter(mBoggerAdapter);
              //  txt.setText("这是新闻Fragment");
                break;
            case 4:
                mLvSynthe.setAdapter(mRecommendAdapter);
              //  txt.setText("这是热点Fragment");
                break;
        }
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        // TODO: inflate a fragment view
//        View rootView = super.onCreateView(inflater, container, savedInstanceState);
//
//        return rootView;
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoadMoreItems() {
        Log.d("aaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaa");
        mPresenter.getData(index++);

        mBoggerPresenter.getData(indexBlog++);

        mHotspotPresenter.getData(indexHot++);

        mRecommendPresenter.getData(indexRec++);
    }
}
